package com.lohit.dbtest;

import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {

	public static final String PREFS_NAME = "LoginCredentials";
	
	

	EditText usernameText, passwordText;

	TextView regTV;

	Button loginBtn, cancelBtn;

	DbController controller = new DbController(this);

	// SchemaHelper helper = new SchemaHelper(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		usernameText = (EditText) findViewById(R.id.username);

		passwordText = (EditText) findViewById(R.id.pass);

		loginBtn = (Button) findViewById(R.id.logBtn);

		// cancelBtn = (Button) findViewById(R.id.canBtn);

		regTV = (TextView) findViewById(R.id.regTextView);

		usernameText.requestFocus();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void cancel(View v) {

		usernameText.setText("");
		passwordText.setText("");

		usernameText.requestFocus();

	}

	public void login(View v) {

		String username = usernameText.getText().toString().trim();

		String password = passwordText.getText().toString().trim();

		boolean inputs = validate(username, password);

		if (inputs) {

			HashMap<String, String> status = controller.loginUser(username, password);

			

			if (!(status==null)) {

				SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
				
				String id = status.get("id");
				String name = status.get("name");
				
				
				
				
				
				SharedPreferences.Editor editor = settings.edit();
				
				
				
				editor.putString("logged", "logged");
				editor.putString("id", id);
				editor.putString("name", name);
				editor.commit();

				Intent i = new Intent(LoginActivity.this, MenuActivity.class);
				startActivity(i);

			} else {

				Toast.makeText(LoginActivity.this,
						"Username or password is invalid! Please try again ",
						Toast.LENGTH_SHORT).show();

				usernameText.setText("");
				passwordText.setText("");
				usernameText.requestFocus();

			}

		}

	}

	public boolean validate(String username, String password) {

		if (username.equals("") || username.length() == 0) {

			Toast.makeText(LoginActivity.this, "Please enter username ",
					Toast.LENGTH_SHORT).show();

			usernameText.requestFocus();

			return false;

		} else if (password.equals("") || password.length() == 0) {

			Toast.makeText(LoginActivity.this, "Please enter password ",
					Toast.LENGTH_SHORT).show();

			passwordText.requestFocus();

			return false;

		}

		return true;
		

	}

	public void register(View v) {

		Intent i = new Intent(LoginActivity.this, RegistrationActivity.class);
		startActivity(i);
		
	}

}
